/*
 * JTablePersonal.java
 *
 * Created on 19 de febrero de 2006, 23:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.gui.jTablePersonal;

import fmap.gui.actionListener.ListenerJTablePersonal;
import fmap.model.Node;
import java.awt.Component;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 *
 * @author Javier Sanroman
 */
public class JTablePersonal extends JTable
{
//    JPopupMenu popup;
    /** Creates a new instance of JTablePersonal */
    public JTablePersonal ()
    {
        /* Eliminamos las lineas entre celdas */
        setShowVerticalLines (false);
        setShowHorizontalLines (false);
        
        // define the popup
/*
        popup = new JPopupMenu ();
        fmap.gui.actionListener.ListenerPopupJTablePersonal listener = new fmap.gui.actionListener.ListenerPopupJTablePersonal ();
        JMenuItem mi = new JMenuItem ("Propiedades");
        mi.addActionListener (listener);
        mi.setActionCommand ("propiedades");
        popup.add (mi);
 
        popup.setOpaque (true);
        popup.setLightWeightPopupEnabled (true);
 */
    }
/*
    protected void processMouseEvent ( MouseEvent evt )
    {
        if( evt.isPopupTrigger () )
        {
//          Con la posicion del puntero del raton averiguamos el treepath
//          TreePath nodoSeleccionado = super.getPathForLocation (evt.getX (),evt.getY ());
 
            int rowSelect       = super.getSelectedRow ();
 
            // Si hemos seleccionado una fila
            if( rowSelect != -1 )
            {
                Object nodo = super.getValueAt ( rowSelect, 0);
 
                // Si el objeto del jtable sobre el que nos encontramos en de tipo Node lo seleccionamos y abrimos el menu popup
                if( nodo instanceof fmap.model.Node )
                {
                    popup.show ( evt.getComponent (),evt.getX (),evt.getY () );
                }
            }
        }
        else
        {
            super.processMouseEvent ( evt );
        }
    }
 */
    public void setData ( Vector data )
    {
        Vector v = new Vector ();
        v.add ("Nombre");
        v.add ("Tamaño");
        v.add ("Fecha");
        
//        TableModelPersonal dtm = new TableModelPersonal ( data, v );
        TableModelPersonal dtm = new TableModelPersonal (  );
        
        dtm.setDataVector ( data, v );
        
        setModel ( dtm );
        
        
        // Asignamos el listener
        
        ListenerJTablePersonal listener = new ListenerJTablePersonal ();
        getSelectionModel ().addListSelectionListener (listener);
        getColumnModel ().getSelectionModel ().addListSelectionListener (listener);
        addMouseListener (listener);
        addKeyListener ( listener );
        
        
//        SortButtonRenderer renderer = new SortButtonRenderer ();
        TableColumnModel model = getColumnModel ();
        
        int colSize[] = {450,30,30};
        int n = 3;
        for (int i=0;i<n;i++)
        {
//            model.getColumn (i).setHeaderRenderer (renderer);
            model.getColumn (i).setPreferredWidth (colSize[i]);
        }
        
        if(data.size ()>0)
        {
            TableColumnModel tcm = getColumnModel ();
            TableColumn tc  = tcm.getColumn (0);
            TableColumn tc1 = tcm.getColumn (1);
            TableColumn tc2 = tcm.getColumn (2);
            tc.setCellRenderer (new MiRendererDeTable ());
            tc1.setCellRenderer (new MiRendererDeTable2 ());
            tc2.setCellRenderer (new MiRendererDeTable2 ());
        }
    }
    
    
    
    
    /**
     * Clase para modificar la apariencia de la columna 0 y asi hacer que aparezca un icono
     *
     * En esta direcci�n encontr� la informaci�n clave para saber como utilizar esto
     * http://www-128.ibm.com/developerworks/java/library/j-jtable/
     * The renderer associated with this class allows you to display instances of ImageIcon within a table.
     * The renderer is simply an instance of DefaultTableCellRenderer that takes advantage of the fact that a
     * JLabel can contain both text and an icon. Instead of rendering the cell by setting its text value,
     * this renderer sets its icon instead.
     */
    private class MiRendererDeTable extends DefaultTableCellRenderer
    {
        private boolean seleccionado;
        
        public MiRendererDeTable ()
        {
        }
        
        public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            Node n = (Node)value;
            
            /** Dependiendo de si es un directorio, fichero o cd mostramos un icono diferente */
            setIcon ( new ImageIcon ( getClass ().getResource (n.getIcon ())));
            setText (n.toString ());
            
            
            setForeground (UIManager.getColor ("Table.selectionForeground"));
            
            this.setFont (new java.awt.Font ("Dialog", 0, 12));
            
            if( !isSelected )   setBackground (UIManager.getColor ("Table.background"));
            else                setBackground (UIManager.getColor ("Table.selectionBackground"));
            
            return( this);
            
        }//public Component getTableCellRendererComponent
        
    }//class
    
    
    /**
     * Para modificar la apariencia de las columnas 1 y 2 y que parezca que no son seleccionables
     */
    private class MiRendererDeTable2 extends DefaultTableCellRenderer
    {
        
        public MiRendererDeTable2 ()
        {
        }
        
        public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            setBackground (UIManager.getColor ("Table.background"));
            
            String prueba = (String)value;
            
            setText ( prueba );
            
            if( column==1 )      this.setHorizontalAlignment (SwingConstants.RIGHT);
            else if( column==2 ) this.setHorizontalAlignment (SwingConstants.CENTER);
            
            this.setBorder (new EmptyBorder (0, 0, 0, 10) );
            this.setFont (new java.awt.Font ("Dialog", 0, 11));
            
            return( this);
        }
        
    }//public class MiRendererDeTable2
    
    
}
